package com.zdkj.subway.st.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.st.entity.SwStCleanPlanDraft;
import com.zdkj.subway.st.service.SwStCleanPlanDraftService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@AllArgsConstructor
@RequestMapping("/swStCleanPlanDraft")
@Api(value = "swStCleanPlanDraft", tags = "车票清洗计划")
public class SwStCleanPlanDraftController {
	private final SwStCleanPlanDraftService swStCleanPlanDraftService;

	@GetMapping("/{id}")
	public R getById(@PathVariable String id) {
		return R.ok(swStCleanPlanDraftService.getById(id));
	}

	@GetMapping("/page")
	public R<IPage> getDictPage(Page page, SwStCleanPlanDraft swStCleanPlanDraft) {
		return R.ok(swStCleanPlanDraftService.page(page, Wrappers.query(swStCleanPlanDraft)));
	}

	@SysLog("车票清洗计划制定")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('st_clean_plan_add')")
	public R save(@Valid @RequestBody SwStCleanPlanDraft swStCleanPlanDraft) {
		swStCleanPlanDraft.setPlanOrder(getNumberForPK());
		return R.ok(swStCleanPlanDraftService.save(swStCleanPlanDraft));
	}

	@SysLog("删除车票清洗计划制定")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('st_clean_plan_delete')")
	public R removeById(@PathVariable String id) {
		return R.ok(swStCleanPlanDraftService.removeById(id));
	}

	@PutMapping
	@SysLog("修改车票清洗计划制定")
	@PreAuthorize("@pms.hasPermission('st_clean_plan_update')")
	public R updateById(@Valid @RequestBody SwStCleanPlanDraft swStCleanPlanDraft) {
		return R.ok(swStCleanPlanDraftService.updateById(swStCleanPlanDraft));
	}

	private String getNumberForPK(){
		String id="ST_";
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
		String temp = sf.format(new Date());
		int random=(int) (Math.random()*10000);
		id=temp+random;
		return id;
	}

}
